import request from "@/utils/request.js";
// 分页
export const uploadFile = (data) => {
	let option = {
		method: "POST",
		loading: true,
		contentType: 'multipart/form-data',
		isFile: true
	};
	return request('/api/sale/common/upload', data, option)
}

// export const getWechatCode = () => {
// 	uni.login({
// 		provider: 'weixin',
// 		success: (res) => {
// 			if (res.code) { //微信登录成功 已拿到code
// 				console.log(res.code)

// 				getWechatOpenId({code: wechatCode})
// 			} else {
// 				console.log('登录失败！' + res.errMsg)
// 			}
// 		},
// 		fail: () => {

// 		}
// 	});
// }
//获取openId
export const getWechatCode = (susscessHandle) => {
	uni.login({
		provider: 'weixin',
		success: (res) => {
			if (res.code) { //微信登录成功 已拿到code
				susscessHandle(res.code)
			} else {
				console.log('登录失败！' + res.errMsg)
			}
		},
		fail: () => {
		}
	});
	// uni.setStorageSync('openid', res.data.openid)
}

//获取openId
export const getWechatOpenIdWithCache = (callback) => {
	let openId = uni.getStorageSync('openId')
	if (openId) {
		callback(openId)
		return
	}

	getWechatCode((code)=>{
		let option = {
			method: "POST",
			loading: false
		};
		getWechatOpenId({code: code}).then((res) => {
			let openId = res.data.openId
			uni.setStorageSync('openId', openId)
			callback(openId)
		})
	})
}

//获取openId
export const getWechatOpenId = (data) => {
	let option = {
		method: "POST",
		loading: false
	};
	return request('/api/sale/wechat/app/open/id', data, option)
}

//点击支付按钮
//将openId传给后台接口，获取微信支付所需参数
export const getWechatPayParams = (data) => {
	let option = {
		method: "POST",
		loading: true
	};
	return request('/api/sale/order/wechat/pay', data, option)
}
//调用支付api，发起支付
export const callWechatPay = (params, successHandle) => {
	uni.requestPayment({
		provider: 'wxpay',
		timeStamp: params.timeStamp,
		nonceStr: params.nonceStr,
		package: params.packageVal,
		signType: params.signType,
		paySign: params.paySign,
		success: (res) => {
			if (successHandle){
				successHandle(res)
			}else {
				uni.showToast({
					title: '支付成功'
				})
			}
		},
		fail: (err) => {
			uni.showToast({
				title: '支付失败',
				duration: 1200,
				icon: "error"
			})
		},
		complete: (err) => {
			//this.getOrderStatus()
		}
	});
}


